<?php

class Geo {

    function GetCountry($ID, $FieldName, $ChildFieldName, $SubChildFieldName, $Parent, $Child, $ChildTarget, $SubChildTarget, $AddAction,$mode) {
        $GeoCountries = "SELECT pai_CodElite AS ID, pai_NombreESP AS NAME FROM hd_paises ORDER BY NAME ASC";
        if($Parent>0){$GetCountries = $Parent;}
        if ($mode == 'RD') {
            $Label = $GLOBALS['MYT']->db_array("SELECT pai_NombreESP AS NAME FROM hd_paises WHERE pai_CodElite = $ID");
            $Label = $Label[1]['NAME'];
        } else {
            $Label = '';
        }
        return '{basic_country}: ' . rt_field('select', $mode, $FieldName, $ID, $GeoCountries, $Label, '', '"
	onChange="'.$AddAction.'
        if(replaceUrl(\'' . $ChildTarget . '\',\'_response/call.php?get=Geo/view/viewselect&Field=Region&FieldName=' . $ChildFieldName . '&Child=' . $SubChildFieldName . '&Country=\'+document.getElementById(\'' . $FieldName . '\').value+\'&Region=' . $Child . '&Target=' . $SubChildTarget . '&Mode=' . $mode . '\')==true){
            window.setTimeout(function ReplaceGeoCity(){
            NewProv = 0;
            NewCountry=0;
            NewCity = \'\';
            if(document.getElementById(\'' . $ChildFieldName . '\').selectedIndex>=0){
            NewProv = document.getElementById(\'' . $ChildFieldName . '\').options[document.getElementById(\'' . $ChildFieldName . '\').selectedIndex].value;
           }
            NewCountry = document.getElementById(\'' . $FieldName . '\').options[document.getElementById(\'' . $FieldName . '\').selectedIndex].value;
            replaceUrl(\'' . $SubChildTarget . '\',\'_response/call.php?get=Geo/view/viewselect&Field=City&FieldName=' . $SubChildFieldName . '&City=\'+NewCity+\'&Region=\'+NewProv+\'&Country=\'+NewCountry);
        },1000);
        }');
    }

    function GetRegion($ID, $FieldName, $ChildFieldName, $Parent, $Child, $ChildTarget, $mode) {
        $Label = '';
        if ($Parent > 0) {
            $GeoRegions = "SELECT Prov_ID AS ID, Prov_Provincia AS NAME FROM cp_provincias WHERE Prov_Pais = $Parent ORDER BY NAME ASC";
            if ($mode == 'RD') {
                if (is_numeric($ID)) {
                    $Label = $GLOBALS['MYT']->db_array("SELECT Prov_Provincia AS NAME
		FROM cp_provincias
		WHERE Prov_ID = " . $ID);
                    $Label = $Label[1]['NAME'];
                } else {
                    $Label = $ID;
                }
            }
        } else {
            $GeoRegions = "SELECT Prov_ID AS ID, Prov_Provincia AS NAME FROM cp_provincias WHERE Prov_ID = 0 ORDER BY NAME ASC";
        }
        return '{basic_region}: ' . rt_field('select', $mode, $FieldName, $ID, $GeoRegions, $Label, '', '"
                onchange="replaceUrl(\'' . $ChildTarget . '\',\'_response/call.php?get=Geo/view/viewselect&Field=City&FieldName=' . $ChildFieldName . '&City=' . $Child . '&Region=\'+document.getElementById(\'' . $FieldName . '\').value+\'&Country=' . $Parent . '\');');
    }

    function GetCity($ID, $FieldName, $Parent, $RootParent, $mode) {
        if ($Parent < 1) { $Parent = 0; }
        if ($RootParent < 1) { $RootParent = 0; }
        $Label = '';


        if ($Parent > 0) {
            $GeoCities = "SELECT CiuNumero AS ID, CiuCiudad AS NAME FROM ciudades
			WHERE CiuPais = $RootParent AND Prov_ID = $Parent
			ORDER BY NAME ASC";
        } else {
            $GeoCities = "SELECT CiuNumero AS ID, CiuCiudad AS NAME
			FROM ciudades
			WHERE CiuPais = $RootParent
			ORDER BY NAME ASC";
        }
        if ($mode == 'RD') {
            if (is_numeric($ID)) {
                $Label = $GLOBALS['MYT']->db_array("SELECT CiuCiudad AS NAME
					FROM ciudades WHERE CiuNumero = $ID");
                $Label = $Label[1]['NAME'];
            } else {
                $Label = $ID;
            }
        }


        return '{basic_city}: ' . rt_field('select', $mode, $FieldName, $ID, $GeoCities, $Label, '', '');
    }

}

?>